import { Navigate, Outlet, useLocation } from "react-router-dom"
import { Routes } from "@/shared/consts"
import { useGetMeQuery } from "@/app/user/userApi"

const PrivateRoute = () => {
  const location = useLocation()
  const token = localStorage.getItem("token")
  const { data: user, isLoading } = useGetMeQuery(null, {
    skip: false,
    refetchOnReconnect: true,
  })
  const isAuth = !isLoading && !!user && !!token

  if (isLoading) {
    return null
  }
  return isAuth ? (
    <Outlet />
  ) : (
    <Navigate to={Routes.Auth} state={{ from: location }} />
  )
}
export default PrivateRoute
